var offer_curr_page = 1;
var offers_per_page = 3;
var offers_num_pages = Math.ceil(offers.length / offers_per_page);
if(offers_num_pages == 0){
	offers_num_pages = 1;
}

var request_curr_page = 1;
var requests_per_page = 3;
var requests_num_pages = Math.ceil(requests.length / requests_per_page);
if(requests_num_pages == 0){
	requests_num_pages = 1;
}


function editPageHeight(){
	var tmp = (requests_num > offers_num) ? requests_num : offers_num;
	if (tmp < 1){
		tmp = 1;
	}
	$("#requests-grid").height(120 + (tmp * 170));
	$("#offers-grid").height(120 + (tmp * 170));
	$("#requests-container").height((tmp * 170));
	$("#offers-container").height((tmp * 170));
	$("#vertical-bar").height(120 + (tmp * 170));
}




function makeRequestsPaginator(){
	$("#requests-paginator").paginate({
		count: requests_num_pages,
		start: request_curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			requestsDisplayPage(page);
			positionRequestsPaginator();
			$(".resize-150").load(function(){
				resize($(this), 150);
			});
			editPageHeight();
			requesttriggerTooltip();
			replaceIdByName();
			replaceIdByPic();
		}
	});
}


function requestsDisplayPage(page){			
	var html = '';
	requests_num = 0;
	request_curr_page = page;
	for (var i = 0; i < requests_per_page; i++){
		var counter = ((page - 1) * requests_per_page) + i;
		
		if (typeof(requests[counter]) == "undefined"){
			if(html == ''){
				html += '<br /><span class="medium-text">No requests found. <br /><br /><a href="browse/">Browse </a> for new items now!</span>';
			}
			break;
		}
		requests_num++;
		
		html += '<div class="item-container">';
		html += '<div class="img-container"><a href="itemdescription/' + requests[counter].offer_id + '"><img class="resize-150" src="resources/offer_images_thumbnail/' + requests[counter].item_pic + '" title="'+requests[counter].item_title+'"/></a></div>';
		html += '<div class="cnt-container normal-text">';
		html += '<span class="medium-text"><a href="itemdescription/'+ requests[counter].offer_id + '">' + requests[counter].item_title.substr(0, 20);
		if (requests[counter].item_title.length > 20){
			html += '...';
		}
		html += '</a></span><br />';
		html += 'Status: ' + requests[counter].status + '<br />';
		html += ' Owner: <span class="get-fb-name normal-text">' + requests[counter].owner + '</span><br /><br />';
		html += '<div class="cancel-button"><a href="JavaScript:void(0);" onclick = "cancel_request('+ requests[counter].offer_id +',\''+requests[counter].status+'\')"></a></div>';
		html += '<div class="request-trigger"><img src = "resources/frame/info.gif" width="30px" height="30px" /></div>';
		html += '<div class="request-tooltip normal-text">';
		html += '<span class="normal-text">' + requests[counter].item_title + '</span><br /><br />';
		html += 'Category: '+ requests[counter].category_name + ' - ' + requests[counter].sub_category_name + '<br /><br />';
		html += 'Description: ' + requests[counter].item_desc+'</div>';
		html += '</div>';
		html += '<div style="clear: both;"></div>';
		html += '</div>';
		html += '</div>';
	}
	
	$("#requests-container").html(html);
}

function removeRequest(id){
	for (var i = 0; i < requests.length; i++){
		if(requests[i].offer_id == id){
			requests.splice(i, 1);
			requests_num_pages = Math.ceil(requests.length / requests_per_page);
			if(requests_num_pages == 0){
				requests_num_pages = 1;
			}
			while(request_curr_page > requests_num_pages){
				request_curr_page--;
			}
			makeRequestsPaginator();
			requestsDisplayPage(request_curr_page);
			positionRequestsPaginator();
			$(".resize-150").load(function(){
				resize($(this), 150);
			});
			editPageHeight();
			requesttriggerTooltip();
			replaceIdByName();
			replaceIdByPic();
			
			break;
		}
	}
}

function positionRequestsPaginator(){
	var width = $('#requests-container').width();
	var length = requests_num_pages > 5 ? 5 : requests_num_pages;
	var actual = 60 + 60 + 25 * requests_num_pages;
	$('#requests-paginator').css('margin-left', (width - actual) / 2);
}



function makeOffersPaginator(){
	$("#offers-paginator").paginate({
		count: offers_num_pages,
		start: offer_curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			offersDisplayPage(page);
			positionOffersPaginator();
			$(".resize-150").load(function(){
				resize($(this), 150);
			});
			editPageHeight();
			offertriggerTooltip();
			replaceIdByName();
			replaceIdByPic();
		}
	});
}


function offersDisplayPage(page){			
	var html = '';
	offers_num = 0;
	offer_curr_page = page;
	for (var i = 0; i < offers_per_page; i++){
		var counter = ((page - 1) * offers_per_page) + i;
		
		if (typeof(offers[counter]) == "undefined"){
			if(html == ''){
				html += '<br /><span class="medium-text">Do you have stuff hidden under your bed? <br /><br /><a href="offerform/0">Create </a> a new offer now!</span>';
			}
			break;
		}
		offers_num++;
		
		html += '<div class="item-container">';
		html += '<div class="img-container"><a href="itemdescription/' + offers[counter].offer_id + '"><img class="resize-150" src="resources/offer_images_thumbnail/' + offers[counter].item_pic + '" title="'+offers[counter].item_title+'"/></a></div>';
		html += '<div class="cnt-container normal-text">';
		html += '<span class="medium-text"><a href="itemdescription/'+ offers[counter].offer_id + '">' + offers[counter].item_title.substr(0, 20);
		if(offers[counter].item_title.length > 20){
			html += '...';
		}
		html += '</a></span><br />';
		html += 'Status: ' + offers[counter].status + '<br />';
		html += '<br /><br />';
		html += '<div class="remove-button"><a href="JavaScript:void(0);" onclick = "cancel_offer('+ offers[counter].offer_id +',\''+offers[counter].status+'\')"></a></div>';
		html += '<div class="offer-trigger"><img src = "resources/frame/info.gif" width="30px" height="30px" /></div>';
		html += '<div class="offer-tooltip normal-text">';
		html += '<span class="normal-text">' + offers[counter].item_title + '</span><br /><br />';
		html += 'Category: '+ offers[counter].category_name + ' - ' + offers[counter].sub_category_name + '<br /><br />';
		html += 'Description: ' + offers[counter].item_desc+'</div>';
		html += '</div>';
		html += '<div style="clear: both;"></div>';
		html += '</div>';
		html += '</div>';
	}
	
	$("#offers-container").html(html);
}


function removeOffer(id){
	for (var i = 0; i < offers.length; i++){
		if(offers[i].offer_id == id){
			offers.splice(i, 1);
			offers_num_pages = Math.ceil(offers.length / offers_per_page);
			if(offers_num_pages == 0){
				offers_num_pages = 1;
			}
			while(offer_curr_page > offers_num_pages){
				offer_curr_page--;
			}
			makeOffersPaginator();
			offersDisplayPage(offer_curr_page);
			positionOffersPaginator();
			$(".resize-150").load(function(){
				resize($(this), 150);
			});
			editPageHeight();
			offertriggerTooltip();
			replaceIdByName();
			replaceIdByPic();
			break;
		}
	}
}

function positionOffersPaginator(){
	var width = $('#offers-container').width();
	var length = offers_num_pages > 5 ? 5 : offers_num_pages;
	var actual = 60 + 60 + 25 * length;
	$('#offers-paginator').css('margin-left', (width - actual) / 2);
}


function offertriggerTooltip(){

	$('.offer-trigger').tooltip({
		effect: 'fade',
		position: 'bottom center',
		opacity: 0.90,
		tipClass: 'offer-tooltip',
		offset: [-180, -960],
	});
}

function requesttriggerTooltip(){

	$('.request-trigger').tooltip({
		effect: 'fade',
		position: 'bottom center',
		opacity: 0.90,
		tipClass: 'request-tooltip',
	});	
}

$(function(){
	makeRequestsPaginator();
	makeOffersPaginator();
	requestsDisplayPage(request_curr_page);
	offersDisplayPage(offer_curr_page);
	positionRequestsPaginator();
	positionOffersPaginator();
	editPageHeight();
	offertriggerTooltip();
	requesttriggerTooltip();
});
